/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Presentacion;

import Logica.DatosConexion;
import Logica.Estrategia;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author Fernando
 */
public class Principal extends javax.swing.JFrame {

    /**
     * Creates new form Principal
     */
    public Principal() {
        //this.setExtendedState(MAXIMIZED_BOTH);
        this.setTitle("RMAN Monitor");
        initComponents();
        datosConec = null;
        this.setContentPane(new Conexion(this));
        this.pack();
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation(dim.width / 2 - this.getPreferredSize().width / 2, dim.height / 2 - this.getPreferredSize().height / 2);
        
        //Creación de un archivo en una dirección exacta
        /*String path = "C:" + File.separator + "A" + File.separator + "hi2.txt";
        //(use relative path for Unix systems)
        File f = new File(path);
        //(works for both Windows and Linux)
        f.mkdirs();
        FileWriter escritorArchivo;
        try {
            f.createNewFile();
            File f1= new File(f+".txt");
            escritorArchivo = new FileWriter(f1);
            BufferedWriter bw = new BufferedWriter(escritorArchivo);
            PrintWriter salida = new PrintWriter(bw);
            salida.write("Hola");
            salida.close();
        } catch (IOException ex) {
            Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
        }*/
        
        
            


        levantarlista();
        JOptionPane.showMessageDialog(this, estrategias.toString());

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenuBar1 = new javax.swing.JMenuBar();
        classMenu = new javax.swing.JMenu();
        EXIT = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        classMenu.setText("SERVERS");
        classMenu.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                classMenuMouseClicked(evt);
            }
        });
        classMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                classMenuActionPerformed(evt);
            }
        });
        jMenuBar1.add(classMenu);

        EXIT.setText("EXIT");
        EXIT.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                EXITMouseClicked(evt);
            }
        });
        EXIT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EXITActionPerformed(evt);
            }
        });
        jMenuBar1.add(EXIT);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 680, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 419, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void classMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_classMenuActionPerformed
    }//GEN-LAST:event_classMenuActionPerformed

    private void classMenuMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_classMenuMouseClicked
        //JOptionPane.showMessageDialog(this,this.datosConec.toString());
        this.setContentPane(new Servidores(this.getDatosConec(), this));
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        this.validate();
    }//GEN-LAST:event_classMenuMouseClicked

    private void EXITActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EXITActionPerformed
    }//GEN-LAST:event_EXITActionPerformed

    private void EXITMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_EXITMouseClicked
        try {
            this.actualizarArchivo();
        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(this, "Error al guardar 1");
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(this, "Error al guardar 2");
        }
        this.dispose();
    }//GEN-LAST:event_EXITMouseClicked
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu EXIT;
    private javax.swing.JMenu classMenu;
    private javax.swing.JMenuBar jMenuBar1;
    // End of variables declaration//GEN-END:variables
    private DatosConexion datosConec;
    private ArrayList<Estrategia> estrategias;

    public javax.swing.JMenu getClassMenu() {
        return classMenu;
    }

    public void setClassMenu(javax.swing.JMenu classMenu) {
        this.classMenu = classMenu;
    }

    public DatosConexion getDatosConec() {
        return datosConec;
    }

    public void setDatosConec(DatosConexion datosConec) {
        this.datosConec = datosConec;
    }

    public void levantarlista() {
        setEstrategias((ArrayList<Estrategia>) new ArrayList());
        try {
            Estrategia s = new Estrategia();
            FileInputStream fis = new FileInputStream("archivo.txt");
            while (true) {
                ObjectInputStream ois = new ObjectInputStream(fis);
                s = (Estrategia) ois.readObject();

                if (s != null) {
                    getEstrategias().add(s);
                }

            }

        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(null, "Error levantar 1");
        } catch (IOException | ClassNotFoundException ex) {
        }
    }

    public void actualizarArchivo() throws FileNotFoundException, IOException {

        FileOutputStream fileOut = new FileOutputStream("archivo.txt", false);

        Iterator guarde = getEstrategias().iterator();
        while (guarde.hasNext()) {
            Estrategia ndts = (Estrategia) guarde.next();
            //JOptionPane.showMessageDialog(null, ndts.toString());
            ndts.guardar("archivo.txt");
        }
        fileOut.close();
    }

    /**
     * @return the estrategias
     */
    public ArrayList<Estrategia> getEstrategias() {
        return estrategias;
    }

    /**
     * @param estrategias the estrategias to set
     */
    public void setEstrategias(ArrayList<Estrategia> estrategias) {
        this.estrategias = estrategias;
    }

    public Estrategia buscaEstrategia(String name) {
        for (int i = 0; i < estrategias.size(); i++) {
            if (name.equals(estrategias.get(i).getNombre())) {
                return estrategias.get(i);
            }
        }
        return null;
    }

    public ArrayList<Estrategia> Estrategias(String serv) {
        ArrayList<Estrategia> est = new ArrayList();
        for (int i = 0; i < estrategias.size(); i++) {
            if (serv.equals(estrategias.get(i).getServer())) {
                est.add(estrategias.get(i));
            }
        }
        return est;
    }
}
